home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c++
- Subject: Re: problems reading binary data from file
- Date: Mon, 05 Feb 1996 12:58:41 GMT
- Organization: Netcom
- Message-ID: <3115fd64.299425728@nntp.ix.netcom.com>
- References: <4f4c4s$69q@zeus.rbi.informatik.uni-frankfurt.de>
- NNTP-Posting-Host: ix-dc7-16.ix.netcom.com
- X-NETCOM-Date: Mon Feb 05 4:57:50 AM PST 1996
- X-Newsreader: Forte Agent .99c/16.141
-
- Ferber@zoology.uni-frankfurt.de (Michael Ferber) wrote:
-
- > I have to read/write binary data from/to a file. Up to now I did this with
- > fread and fwrite, but I want to switch to the fstream classes to avoid mixture
- > of c and c++ code. Unfortunately the following code (the first part which
- > writes to temp.bin) writes characters not binaries. What am I doing wrong???
- > The second part (which writes to test.bin) works as expected. I use Watcom
- > 10.0a. The code is a modiffied piece of the Watcom samples provided with the
- > compiler.
- > Thanks in advance
- > Michael
- >
- > #include <fstream.h>
- > #include <sys\stat.h>
- > #include <sys\types.h>
- > #include <fcntl.h>
- > #include <stdio.h>
- >
- > void main( void )
- > {
- >
- >
- > int handle;
- >
- > handle = open( "temp.bin", ios::binary | ios::out , S_IRWXU );
- > fstream test ( handle );
- > for(int k = 1; k <=1000; k++)
- > test << k;
- >
- > FILE *test2;
- > test2 = fopen("test.bin", "w+b");
- > for(int m = 1; m <=1000; m++)
- > fwrite(&m, sizeof(m),1, test2);
- > }
-
- You're using the wrong operator to write out the data. << writes to
- the stream using the text representation of the data even if the
- stream is binary. You want something like
-
- test.write((char*) &k, sizeof k);
-
-
- Michael M Rubenstein
-